package pl.lodz.p.mbus;


import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.xmlpull.v1.XmlPullParserException;


import android.support.v4.app.Fragment;
import android.R.interpolator;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MenuFragment extends Fragment  {

	List<Sensor> vals;
	
	public interface OnMenuClickedListener {
		public void onMenuItemSelected (Sensor s);
	}
	
	OnMenuClickedListener menuCallback;
	
private class CustomListViewAdapter extends ArrayAdapter<Sensor>{
	
		Context context;
		
		public CustomListViewAdapter(Context context, int resourceId,
	            List<Sensor> items) {
	        super(context, resourceId, items);
	        this.context = context;
	    }
		
		 private class ViewHolder {
		        Button button;
		    }
		 
		
		 
		 @Override
		public View getView(int position, View convertView, ViewGroup parent) {
			 ViewHolder holder = null;
		        Sensor rowItem = getItem(position);
		 
		        getActivity();
				LayoutInflater mInflater = (LayoutInflater) context
		                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		        if (convertView == null) {
		            convertView = mInflater.inflate(R.layout.item_view, null);
		            holder = new ViewHolder();
		            holder.button = (Button) convertView.findViewById(R.id.button1);
		            convertView.setTag(holder);
		        } else
		            holder = (ViewHolder) convertView.getTag();
		 
		        holder.button.setText(rowItem.name);
		        holder.button.setMinimumWidth(125);
		        if (rowItem.icon.equals("heating"))
		        	holder.button.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.heating, 0, 0);
		        else if (rowItem.icon.equals("hotwater"))
		        	holder.button.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.hot_water, 0, 0);
		        else if (rowItem.icon.equals("swimmingpool"))
		        	holder.button.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.swimmingpool, 0, 0);
		        else if (rowItem.icon.equals("swimmingpoolair"))
		        	holder.button.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.swimmingpoolair, 0, 0);
		        return convertView;
		}
		
	}
	

		@Override
		public void onAttach(Activity activity) {
			super.onAttach(activity);
			menuCallback = (OnMenuClickedListener) activity;
		}
		
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			// TODO Auto-generated method stub
	        return inflater.inflate(R.layout.menu_fragment_layout, container, false);

		}
		
		@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				InputStream in = getResources().openRawResource(R.raw.menu);
				RoomXmlParser xmlParser = new RoomXmlParser();
				try {
					vals = xmlParser.parse(in);
				} catch (XmlPullParserException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				HorizontalListView listView = (HorizontalListView)getActivity().findViewById(R.id.list);
		        listView.setAdapter(new CustomListViewAdapter(getActivity().getApplicationContext(), 
		                android.R.layout.simple_list_item_1, vals));
		        listView.setOnItemClickListener(new OnItemClickListener() {
		        	public void onItemClick(android.widget.AdapterView<?> arg0, View arg1, int position, long id) {
		        		menuCallback.onMenuItemSelected(vals.get(position));
		        	}
		        });
		        
		        WindowManager wm = (WindowManager)getActivity().getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
		        Display display = wm.getDefaultDisplay();
		        
		        Utility.setListViewHeightBasedOnChildren(listView,display.getWidth());
			}
		
		
		
	
}
